<template>
  <div class="app-container legal-wrap">
    <dynamic-legal-create
      ref="dynamicLegal"
      v-model="legalParam"
      :hide-required-asterisk="false"
      :rules="rules"
      :default-list="defaultList"
      :constructions="comLegalConstruction"
      :flow-structure="processDetail"
      :last-template="lastTemplate"
      :download-loading="downloadLoading"
      @downloadTemplate="downloadTemplate"
      @showPreviewDrawer="showPreviewDrawer"
      @showTemplateDrawer="showTemplateDrawer"/>
    <!-- 关联合同 -->
    <ContractRelevance ref="contractRelevanceRef" v-model="legalParam.contractList" />
    <!-- :old-audit-info="legalDetail.oldAduitInfo" -->
    <audit-process
      ref="auditProcess"
      :old-audit-info="legalDetail.oldAduitInfo"
      :default-aduitor-list="[proposer]"
      v-model="legalParam.aduitInfo"
      :flow-structure="processDetail"
      :is-modify="Boolean(remodifyId)"
      :disable="Boolean(remodifyId)"
      :is-modify-form="Number(isModifyForm)"
      :current-process-index="Number(currentProcessIndex)"
      @submit="submitFun"
      @getProcess="getProcess" />
    <fixed-pagination>
      <div class="center">
        <el-button v-if="!legalId" type="info" size="small" class="back-btn empty-btn-style" @click="toBackStep">上一步</el-button>
        <el-button v-if="setSubmitStatus()" type="primary" size="small" class="submit-btn" @click="submit()">发起审批</el-button>
        <el-button v-if="setSaveDraftStatus()" type="info" size="small" class="back-btn empty-btn-style" @click="saveDraft">保存草稿</el-button>
        <el-button type="info" size="small" class="back-btn cancel-btn-style" @click="toBack">返回列表</el-button>
      </div>
    </fixed-pagination>
    <ContractTemplate
      ref="contractTemplateRef"
      :contract-type="type"
      :preview-path="previewPath"
      :download-loading="downloadLoading"
      @downloadTemplate="downloadTemplate" />
    <ContractPreview
      ref="ContractPreviewRef"
      :file-data="lastTemplate"
      :preview-path="previewPath"
      :download-loading="downloadLoading"
      :show-footer="false"
      is-fill
      @setDownloadParams="downloadPreviewFile"/>
  </div>
</template>
<script>
import DynamicLegalCreate from '../module/dynamicLegalCreate';
import ContractRelevance from '../module/contractRelevance';
import AuditProcess from '@/components/Common/auditProcess';
import Title from '@/components/Title';
import ContractTemplate from '@/components/legal/contractTemplate/index';
import ContractPreview from '@/components/legal/contractTemplate/contractPreview';
import Common from '../mixins/common';
import { investLegalConstruct as legalConstruction } from '../model/legalConstruction';
import { investLegalRules } from '../model/legalRules';
export default {
  name: 'InvestLegal',
  components: { DynamicLegalCreate, AuditProcess, Title, ContractTemplate, ContractPreview, ContractRelevance },
  mixins: [
    Common
  ],
  props: {
    legalDetail: {
      type: Object | String,
      default: () => null
    },
    type: {
      type: String | Number,
      default: ''
    }
  },
  data() {
    return {
      investLegalRules,
      legalParam: {
        createInfo: {
          createrDepartment: '',
          creater: '',
          createrPost: '',
          createrId: '',
          createrDepartmentId: '',
          proposer: null,
          proposerId: '',
          proposerAvatar: '',
          departmentId: '',
          post: '--',
          flowCode: ''
        },
        contractInfo: {
          type: '',
          name: '',
          tempSource: '',
          subject: '',
          oppCompany: '',
          contractDate: '',
          appraisementAmount: '',
          involveAmount: '',
          involveAmountStock: '',
          code: '',
          attachment: [],
          describe: '',
          aaumChangeDate: '',
          businessType: {
            type: '',
            name: ''
          },
          payDates: [{ payDate: '' }],
          // contractCodes: [{ code: '' }],
          ourStamp: '',
          legalTemplate: '',
          stampType: ''
        },
        submitType: 2,
        aduitInfo: {
          countersignSet: 0,
          process: [],
          senders: []
        },
        contractList: [] // 关联合同列表
      },
      defaultList: {
        'createInfo.proposer': []
      },
      comLegalConstruction: []
    };
  },
  computed: {
    /* comLegalConstruction() {
      return legalConstruction(this.legalConstructionArg);
    }, */
    rules() {
      const tempRules = JSON.parse(JSON.stringify(this.investLegalRules));
      // const accountPeriodType = this.legalParam.contractInfo.accountPeriod.type;
      // if (accountPeriodType === 4) {
      //   tempRules['contractInfo.accountPeriod.name'][0].required = true;
      // } else {
      //   tempRules['contractInfo.accountPeriod.name'][0].required = false;
      // }
      return tempRules;
    }
  },
  created() {
    this.comLegalConstruction = JSON.parse(JSON.stringify(legalConstruction(this.legalConstructionArg)));
  },
  methods: {
    submitFun(senders = []) {
      this.modifySenders = senders;// 存到mixins
      var timestamp = Date.parse(new Date());
      if (timestamp - this.antiShakeTime < 600) {
        return;
      }
      this.antiShakeTime = timestamp;
      if (this.isSubmit) return;
      const params = JSON.parse(JSON.stringify(this.legalParam));
      if (this.reapplyId) {
        params.submitType = 3;
        params.byReapplyId = this.legalId;
      } else {
        params.submitType = 2;
        if (params.hasOwnProperty('byReapplyId')) delete params.byReapplyId;
      }
      this.remodifyId && (params.byRemodifyId = this.remodifyId);
      params.orders = this.orderPriceSwitch(this.orders, 2);
      delete params.createInfo.creater;
      delete params.createInfo.createrDepartment;
      delete params.createInfo.proposer;
      if (this.isSigner === 0) {
        params.aduitInfo = this.removeSigner(params.aduitInfo);
      }
      const message = (this.legalId && !this.createNew && !this.setSaveDraftStatus()) ? '合同修改成功' : '合同新建成功';
      if (this.legalId && !this.createNew) {
        this.handleModifyData(params);
      }
      this.handlePost(params, message);
    }
  }
};
</script>
<style lang="scss" scoped>
.legal-wrap {
  .footer-btn-wrap {
    display: flex;
    justify-content: center;
  }
  .module-box {
    // margin-bottom: 40px;
    padding-bottom: 32px;
  }
}
.empty-btn-style {
  border-color: #406EFF;
  background: #fff;
  color: #406EFF;
  width: 80px;
  padding: 7px 0;
  text-align: center;
}
::v-deep .el-form-item__error {
  padding-top: 2px !important;
}
</style>
